/*
* @Description: 处理vue端发送的请求，根据应用类型分别处理， app类型转为DBRequest
            处理， web类型转为WEBRequest.
* @Author: zht
* @Date: 2023-01-19
* @LastEditTime: 2023-01-19
* @LastEditors: zht
* 佛祖保佑      永无BUG
*/
import axios from 'axios';
import { isEmpty } from 'lodash';
// import type { responseType, requestType } from "@/common/req";
import { useSysStore } from '@/store/sys';
import { ElMessage } from 'element-plus';
import { FuncURLMap } from '@/common/const';

export type responseType = {
    code: number;
    msg?: string;
    obj?: any;
    ext?: any;
};

// import axios from 'axios';
/**
 * request请求体基础类
 */
export class Request<T> {
    url: string = '';
    oper: number;
    preApi = import.meta.env.DEV ? '/api' : '';
    sysStore = useSysStore();
    r: responseType = {
        code: 0,
        msg: '',
        obj: null,
    };
    // 声明参数属性
    private func: string;
    private body?: T;

    constructor(
        func: string,
        body?: T,
    ) {
        this.func = func;
        this.body = body;
        this.oper = this.sysStore.oper!.id as number;
        this.body = body;
        this.url = FuncURLMap[func] as string;
    }
    private checkReq(): void {
        if (isEmpty(this.func)) {
            this.r.code = -1;
            this.r.msg = '关键参数功能不能为空';
        }
        if (this.func !== '登入' && this.func !== '验证码' && !this.oper) {
            this.r.code = -2;
            this.r.msg = '关键参数操作员不能为空';
        }
        if (!this.url) {
            this.r.code = -3;
            this.r.msg = `没有${this.func}权限XXX`;
        }
        if (this.r.msg!.length > 0) {
            ElMessage({
                dangerouslyUseHTMLString: true,
                duration: 0,
                message: this.r.msg,
                showClose: true,
                type: 'error',
            });
        }
    }
    private showResponseMeg(resObj: any): void {
        /**
         * 
         * 301-399 : 操作成功需要info提示
         * 201-299 : 操作成功需要success提示
         * 101-199 : 业务成功不需要提示
         * 1-99    : 业务失败需要error提示
         * < 0     : 系统错误需要提示
         */
        if (resObj.code > 300 && resObj.code < 400) {
            ElMessage({
                type: 'info',
                dangerouslyUseHTMLString: true,
                message: resObj.msg.join('<br><br>')
            })
        }
        if (resObj.code > 200 && resObj.code < 300) {
            ElMessage({
                type: 'success',
                dangerouslyUseHTMLString: true,
                message: resObj.msg.join('<br><br>')
            })
        }
        if (resObj.code > 100 && resObj.code < 200) {
            null;
        }
        if (resObj.code > 0 && resObj.code < 100) {
            ElMessage({
                dangerouslyUseHTMLString: true,
                duration: 0,
                message: resObj.msg.join('<br><br>'),
                showClose: true,
                type: 'error',
            });
        }
        if (resObj.code < 0) {
            ElMessage({
                dangerouslyUseHTMLString: true,
                duration: 0,
                message: '程序错误，请联系管理员',
                showClose: true,
                type: 'error',
            });
        }
    }
    async post(): Promise<responseType> {
        this.checkReq();
        // console.log(this.r);
        if (this.r.code < 0) {
            return this.r;
        }
        try {
            const result = await axios.post(this.preApi + this.url, this.body/* {
                func: this.func,
                oper: this.oper,
                body: this.body,
            } */).catch((error) => {
                console.log('error', error);
                ElMessage({
                    dangerouslyUseHTMLString: true,
                    duration: 0,
                    message: '程序错误1,请联系管理员',
                    showClose: true,
                    type: 'error',
                });
                return error
            });
            this.showResponseMeg(result.data);
            return result.data;
        } catch (error) {
            ElMessage({
                dangerouslyUseHTMLString: true,
                duration: 0,
                message: '程序错误，请联系管理员',
                showClose: true,
                type: 'error',
            });
            console.log(error);
            return this.r;
        }
    }
    async get(): Promise<responseType> {
        this.checkReq();
        // console.log(this.r);
        if (this.r.code < 0) {
            // ElMessage.info(r.msg);
            return this.r;
        }
        try {
            const result = await axios.get(this.preApi + this.url, {
                params: this.body/* {
                    func: this.func,
                    oper: this.oper,
                    body: this.body,
                } */,
            });
            this.showResponseMeg(result.data);
            // console.log(result.data);
            return result.data;
        } catch (error) {
            ElMessage({
                dangerouslyUseHTMLString: true,
                duration: 0,
                message: '程序错误，请联系管理员',
                showClose: true,
                type: 'error',
            });
            console.log(error);
            return this.r;
        }
    }
}
